package com.wanho.qz103.hrsaas.sys.service.impl;

import com.wanho.qz103.hrsaas.commons.annontations.Autowired;
import com.wanho.qz103.hrsaas.commons.annontations.Service;
import com.wanho.qz103.hrsaas.commons.annontations.Transactional;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.util.IDUtils;
import com.wanho.qz103.hrsaas.sys.dao.DepartmentDAO;
import com.wanho.qz103.hrsaas.sys.pojos.Department;
import com.wanho.qz103.hrsaas.sys.service.DepartmentService;
import com.wanho.qz103.hrsaas.sys.vo.DepartmentVO;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDAO departmentDAO ;

    @Override
    public boolean remove(String id) throws ServiceException {
        try {
            return departmentDAO.delete(id);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("删除部门信息异常！！！") ;
        }
    }

    @Override
    public boolean modify(Department department) throws ServiceException {
        try {
            return departmentDAO.update(department);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("修改部门信息异常！！！") ;
        }
    }

    @Override
    public Department findById(String id) throws ServiceException {
        try {
            return departmentDAO.selectById(id);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("查询部门列表异常！！！") ;
        }
    }

    @Override
    public DepartmentVO list() throws ServiceException {
        try {
            //获得所有的部门信息
            List<Department> departmentList = departmentDAO.selectAll();
            //过滤查询 第一个 父id 为null
            Optional<Department> optional = departmentList.stream().filter(department -> department.getPid()==null).findFirst();
            // 删除根
            departmentList.removeIf(department -> department.getPid()==null) ;
            //获得树 的根元素 root
            Department rootDepartment = optional.get();
            // 封装vo
            DepartmentVO departmentVO = new DepartmentVO();
            // 把根元素的属性 映射 vo 属性
            departmentVO.setCompanyId(rootDepartment.getId());
            departmentVO.setCompanyManage(rootDepartment.getManager());
            departmentVO.setCompanyName(rootDepartment.getName());
            // 其他所有部门
            departmentVO.setDepts(departmentList);
            // 返回vo对象
            return departmentVO;
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("查询部门列表异常！！！") ;
        }
    }

    @Override
    public boolean save(Department department) throws ServiceException {
        try {
            department.setId(IDUtils.randId());
            return departmentDAO.insert(department);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("新增部门业务异常！") ;
        }
    }
}
